package com.umakr.ax.security.model;

import com.umakr.ax.security.service.RoleService;
import com.umakr.ax.utils.WebUtil;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* @author gx
* @since 2017/4/25 15:56
*/
public class SecurityUser extends User implements UserDetails {

    public SecurityUser(User sUser){
        if(sUser != null){
            this.setId(sUser.getId());
            this.setPassword(sUser.getPassword());
            this.setAccount(sUser.getAccount());
        }
    }
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        List<Role> sRoleSet  = WebUtil.getBean(RoleService.class).queryRoleByUserId(this.getId());
        if(sRoleSet!=null){
            for(Role role: sRoleSet){
                SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role.getRoleCode());
                authorities.add(authority);
            }
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return super.getPassword();
    }

    @Override
    public String getUsername() {
        return super.getAccount();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
